package com.example.bad;

import java.util.Optional;

import java.util.*;

/**
 * 包含各种代码异味的糟糕示例
 * - 大量魔法值
 * - 重复代码
 * - public字段
 * - 长方法
 * - 复杂条件逻辑
 */
public class BadCodeExample {
    
    // 封装的private字段
    private String companyName = "TechCorp";
    private int employeeCount = 500;
    private double revenue = 1000000.0;
    private boolean isPublicCompany = true;
    private String address = "123 Main Street";
    private String phone = "555-1234";
    
    // Getter和Setter方法
    public String getCompanyName() {
        return companyName;
    }
    
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
    
    public int getEmployeeCount() {
        return employeeCount;
    }
    
    public void setEmployeeCount(int employeeCount) {
        this.employeeCount = employeeCount;
    }
    
    public double getRevenue() {
        return revenue;
    }
    
    public void setRevenue(double revenue) {
        this.revenue = revenue;
    }
    
    public boolean isPublicCompany() {
        return isPublicCompany;
    }
    
    public void setPublicCompany(boolean publicCompany) {
        isPublicCompany = publicCompany;
    }
    
    public String getAddress() {
        return address;
    }
    
    public void setAddress(String address) {
        this.address = address;
    }
    
    public String getPhone() {
        return phone;
    }
    
    public void setPhone(String phone) {
        this.phone = phone;
    }
    
    // 大量魔法值和重复代码
    public double calculateSalary(String level, int experience, String department) {
        double baseSalary = 0;
        
        // 魔法值：职级基础薪资
        if (level.equals("JUNIOR")) {
            baseSalary = 50000;
        } else if (level.equals("SENIOR")) {
            baseSalary = 80000;
        } else if (level.equals("LEAD")) {
            baseSalary = 120000;
        } else if (level.equals("MANAGER")) {
            baseSalary = 150000;
        } else if (level.equals("DIRECTOR")) {
            baseSalary = 200000;
        } else {
            baseSalary = 40000; // 默认薪资
        }
        
        // 魔法值：经验加成
        if (experience >= 1 && experience <= 2) {
            baseSalary = baseSalary * 1.1; // 10%加成
        } else if (experience >= 3 && experience <= 5) {
            baseSalary = baseSalary * 1.2; // 20%加成
        } else if (experience >= 6 && experience <= 10) {
            baseSalary = baseSalary * 1.3; // 30%加成
        } else if (experience > 10) {
            baseSalary = baseSalary * 1.5; // 50%加成
        }
        
        // 魔法值：部门系数
        if (department.equals("ENGINEERING")) {
            baseSalary = baseSalary * 1.2;
        } else if (department.equals("SALES")) {
            baseSalary = baseSalary * 1.15;
        } else if (department.equals("MARKETING")) {
            baseSalary = baseSalary * 1.1;
        } else if (department.equals("HR")) {
            baseSalary = baseSalary * 1.05;
        } else if (department.equals("FINANCE")) {
            baseSalary = baseSalary * 1.1;
        }
        
        return baseSalary;
    }
    
    public double calculateBonus(String level, int experience, String department, double performance) {
        double baseBonus = 0;
        
        // 重复的魔法值
        if (level.equals("JUNIOR")) {
            baseBonus = 5000;
        } else if (level.equals("SENIOR")) {
            baseBonus = 8000;
        } else if (level.equals("LEAD")) {
            baseBonus = 12000;
        } else if (level.equals("MANAGER")) {
            baseBonus = 15000;
        } else if (level.equals("DIRECTOR")) {
            baseBonus = 20000;
        } else {
            baseBonus = 3000;
        }
        
        // 重复的经验逻辑
        if (experience >= 1 && experience <= 2) {
            baseBonus = baseBonus * 1.1;
        } else if (experience >= 3 && experience <= 5) {
            baseBonus = baseBonus * 1.2;
        } else if (experience >= 6 && experience <= 10) {
            baseBonus = baseBonus * 1.3;
        } else if (experience > 10) {
            baseBonus = baseBonus * 1.5;
        }
        
        // 重复的部门逻辑
        if (department.equals("ENGINEERING")) {
            baseBonus = baseBonus * 1.2;
        } else if (department.equals("SALES")) {
            baseBonus = baseBonus * 1.15;
        } else if (department.equals("MARKETING")) {
            baseBonus = baseBonus * 1.1;
        } else if (department.equals("HR")) {
            baseBonus = baseBonus * 1.05;
        } else if (department.equals("FINANCE")) {
            baseBonus = baseBonus * 1.1;
        }
        
        // 绩效系数魔法值
        if (performance >= 0.9) {
            baseBonus = baseBonus * 1.5; // 优秀
        } else if (performance >= 0.8) {
            baseBonus = baseBonus * 1.3; // 良好
        } else if (performance >= 0.7) {
            baseBonus = baseBonus * 1.1; // 合格
        } else if (performance >= 0.6) {
            baseBonus = baseBonus * 0.8; // 待改进
        } else {
            baseBonus = 0; // 不合格
        }
        
        return baseBonus;
    }
    
    // 另一个包含魔法值的方法
    public String getEmployeeLevel(int experience, double performance) {
        if (experience < 2) {
            return "JUNIOR";
        } else if (experience >= 2 && experience < 5) {
            if (performance >= 0.8) {
                return "SENIOR";
            } else {
                return "JUNIOR";
            }
        } else if (experience >= 5 && experience < 8) {
            if (performance >= 0.85) {
                return "LEAD";
            } else {
                return "SENIOR";
            }
        } else if (experience >= 8 && experience < 12) {
            if (performance >= 0.9) {
                return "MANAGER";
            } else {
                return "LEAD";
            }
        } else {
            if (performance >= 0.95) {
                return "DIRECTOR";
            } else {
                return "MANAGER";
            }
        }
    }
    
    // 包含字符串常量模式的方法（适合提取枚举）
    public String getStatusDescription(String status) {
        if (status.equals("ACTIVE")) {
            return "员工状态：在职";
        } else if (status.equals("INACTIVE")) {
            return "员工状态：离职";
        } else if (status.equals("PENDING")) {
            return "员工状态：待入职";
        } else if (status.equals("SUSPENDED")) {
            return "员工状态：停职";
        } else if (status.equals("TERMINATED")) {
            return "员工状态：解雇";
        } else {
            return "员工状态：未知";
        }
    }
    
    public String getDepartmentCode(String department) {
        if (department.equals("ENGINEERING")) {
            return "ENG";
        } else if (department.equals("SALES")) {
            return "SAL";
        } else if (department.equals("MARKETING")) {
            return "MKT";
        } else if (department.equals("HR")) {
            return "HR";
        } else if (department.equals("FINANCE")) {
            return "FIN";
        } else {
            return "UNK";
        }
    }
    
    // 包含数字常量的方法
    public boolean isEligibleForPromotion(int currentLevel, int experience) {
        // 魔法数字：晋升条件
        if (currentLevel == 1 && experience >= 24) { // 24个月
            return true;
        } else if (currentLevel == 2 && experience >= 36) { // 36个月
            return true;
        } else if (currentLevel == 3 && experience >= 48) { // 48个月
            return true;
        } else if (currentLevel == 4 && experience >= 60) { // 60个月
            return true;
        } else {
            return false;
        }
    }
    
    // 复杂的for循环（可以优化为Stream）
    public List<String> filterEmployeesByDepartment(List<Employee> employees, String targetDepartment) {
        List<String> result = new ArrayList<>();
        for (Employee emp : employees) {
            if (Optional.ofNullable(emp).flatMap(o -> Optional.ofNullable(o.getDepartment())).isPresent()) {
                if (emp.getDepartment().equals(targetDepartment)) {
                    result.add(emp.getName());
                }
            }
        }
        return result;
    }
    
    public double calculateAverageSalary(List<Employee> employees) {
        double total = 0;
        int count = 0;
        for (Employee emp : employees) {
            if (emp != null && emp.getSalary() > 0) {
                total += emp.getSalary();
                count++;
            }
        }
        return count > 0 ? total / count : 0;
    }
    
    // 匿名类（可以改为Lambda）
    public List<Employee> sortEmployeesBySalary(List<Employee> employees) {
        Collections.sort(employees, (Employee e1, Employee e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        return employees;
    }
    
    // null检查（可以用Optional优化）
    public String getEmployeeInfo(Employee employee) {
        if (employee != null) {
            if (employee.getName() != null) {
                if (employee.getDepartment() != null) {
                    return employee.getName() + " - " + employee.getDepartment();
                } else {
                    return employee.getName() + " - 未知部门";
                }
            } else {
                return "匿名员工";
            }
        } else {
            return "员工信息不存在";
        }
    }
    
    // 大方法：处理员工数据（超过80行）
    public EmployeeReport processEmployeeData(List<Employee> employees, String reportType) {
        System.out.println("开始处理员工数据...");
        
        if (employees == null || employees.isEmpty()) {
            throw new IllegalArgumentException("员工列表不能为空");
        }
        
        EmployeeReport report = new EmployeeReport();
        report.setReportType(reportType);
        report.setGeneratedDate(new Date());
        
        int totalEmployees = 0;
        double totalSalary = 0;
        double maxSalary = 0;
        double minSalary = Double.MAX_VALUE;
        
        Map<String, Integer> departmentCount = new HashMap<>();
        Map<String, Double> departmentSalarySum = new HashMap<>();
        
        List<Employee> highPerformers = new ArrayList<>();
        List<Employee> lowPerformers = new ArrayList<>();
        
        for (Employee emp : employees) {
            if (emp == null) continue;
            
            totalEmployees++;
            
            // 薪资统计
            double salary = emp.getSalary();
            totalSalary += salary;
            
            if (salary > maxSalary) {
                maxSalary = salary;
            }
            
            if (salary < minSalary) {
                minSalary = salary;
            }
            
            // 部门统计
            String dept = emp.getDepartment();
            if (dept != null) {
                departmentCount.put(dept, departmentCount.getOrDefault(dept, 0) + 1);
                departmentSalarySum.put(dept, departmentSalarySum.getOrDefault(dept, 0.0) + salary);
            }
            
            // 绩效分类
            double performance = emp.getPerformance();
            if (performance >= 0.8) {
                highPerformers.add(emp);
            } else if (performance < 0.6) {
                lowPerformers.add(emp);
            }
        }
        
        // 计算平均薪资
        double avgSalary = totalEmployees > 0 ? totalSalary / totalEmployees : 0;
        
        // 设置报告数据
        report.setTotalEmployees(totalEmployees);
        report.setTotalSalary(totalSalary);
        report.setAverageSalary(avgSalary);
        report.setMaxSalary(maxSalary);
        report.setMinSalary(minSalary);
        report.setDepartmentCount(departmentCount);
        report.setHighPerformers(highPerformers);
        report.setLowPerformers(lowPerformers);
        
        // 计算部门平均薪资
        Map<String, Double> departmentAvgSalary = new HashMap<>();
        for (String dept : departmentCount.keySet()) {
            int count = departmentCount.get(dept);
            double sum = departmentSalarySum.get(dept);
            departmentAvgSalary.put(dept, count > 0 ? sum / count : 0);
        }
        report.setDepartmentAvgSalary(departmentAvgSalary);
        
        System.out.println("员工数据处理完成");
        
        return report;
    }
    
    // 内部类
    public static class Employee {
        private String name;
        private String department;
        private double salary;
        private double performance;
        
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        
        public String getDepartment() { return department; }
        public void setDepartment(String department) { this.department = department; }
        
        public double getSalary() { return salary; }
        public void setSalary(double salary) { this.salary = salary; }
        
        public double getPerformance() { return performance; }
        public void setPerformance(double performance) { this.performance = performance; }
    }
    
    public static class EmployeeReport {
        private String reportType;
        private Date generatedDate;
        private int totalEmployees;
        private double totalSalary;
        private double averageSalary;
        private double maxSalary;
        private double minSalary;
        private Map<String, Integer> departmentCount;
        private Map<String, Double> departmentAvgSalary;
        private List<Employee> highPerformers;
        private List<Employee> lowPerformers;
        
        // getter和setter方法
        public String getReportType() { return reportType; }
        public void setReportType(String reportType) { this.reportType = reportType; }
        
        public Date getGeneratedDate() { return generatedDate; }
        public void setGeneratedDate(Date generatedDate) { this.generatedDate = generatedDate; }
        
        public int getTotalEmployees() { return totalEmployees; }
        public void setTotalEmployees(int totalEmployees) { this.totalEmployees = totalEmployees; }
        
        public double getTotalSalary() { return totalSalary; }
        public void setTotalSalary(double totalSalary) { this.totalSalary = totalSalary; }
        
        public double getAverageSalary() { return averageSalary; }
        public void setAverageSalary(double averageSalary) { this.averageSalary = averageSalary; }
        
        public double getMaxSalary() { return maxSalary; }
        public void setMaxSalary(double maxSalary) { this.maxSalary = maxSalary; }
        
        public double getMinSalary() { return minSalary; }
        public void setMinSalary(double minSalary) { this.minSalary = minSalary; }
        
        public Map<String, Integer> getDepartmentCount() { return departmentCount; }
        public void setDepartmentCount(Map<String, Integer> departmentCount) { this.departmentCount = departmentCount; }
        
        public Map<String, Double> getDepartmentAvgSalary() { return departmentAvgSalary; }
        public void setDepartmentAvgSalary(Map<String, Double> departmentAvgSalary) { this.departmentAvgSalary = departmentAvgSalary; }
        
        public List<Employee> getHighPerformers() { return highPerformers; }
        public void setHighPerformers(List<Employee> highPerformers) { this.highPerformers = highPerformers; }
        
        public List<Employee> getLowPerformers() { return lowPerformers; }
        public void setLowPerformers(List<Employee> lowPerformers) { this.lowPerformers = lowPerformers; }
    }
}